package com.csist.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csist.pojo.Events;
import com.csist.service.impl.EventsServiceImpl;
import com.csist.utils.Upload;
import com.fasterxml.jackson.databind.util.JSONPObject;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 活动 前端控制器【done】
 *
 * @author wy
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/events")
public class EventsController {
    @Autowired
    EventsServiceImpl eventsService;

    @Value("${myConfig.eventsPageNum}")
    private int pageNum;
    @Value("${myConfig.annexPath}")
    private String annexPath;
    @Value("${myConfig.eventsCoverImgPath}")
    private String eventsCoverPath;



    /**
     * 新增/修改 活动
     * @param events 活动实体对象
     * @return true:新增/修改成功,false:新增/修改失败
     */
    @RequestMapping("/add")
    public boolean addEvents(@RequestBody Events events){
        events.setWssbjssj(events.getWssbjssj().plusHours(8));
        events.setWssbkssj(events.getWssbkssj().plusHours(8));
        return eventsService.saveOrUpdate(events);
    }

    /**
     * 附件上传
     * @param file 文件
     * @return "filed":上传失败,上传完成后的文件绝对地址:上传成功
     */
    @RequestMapping("/annexUpload")
    public String annexUpload(MultipartFile file){
        return Upload.upload(file,annexPath);
    }

    /**
     * 活动(封面)图片上传
     * @param file 文件
     * @return "filed":上传失败,上传完成后的文件绝对地址:上传成功
     */
    @RequestMapping("/eventsCoverUpload")
    public String eventsCoverUpload(MultipartFile file){
        return Upload.upload(file,eventsCoverPath);
    }


    /**
     * 根据活动id获取活动数据
     * @param id 活动id
     * @return 活动数据
     */
    @RequestMapping("/getOne")
    public Events getEventsById(int id){
       return eventsService.getById(id);
    }

    /**
     * 根据id集合删除活动数据
     * @param ids id集合
     * @return true:删除成功,false删除失败
     */
    @RequestMapping("/delete")
    public Boolean deleteEventByIds(@RequestBody List<Integer> ids){
        return eventsService.removeByIds(ids);
    }

    /**
     * 根据标题查询活动
     * @param title 查询标题
     * @param index 页码(在application.yaml的eventsPageNum中配置一页可加载数据条数)
     * @return 活动集合
     */
    @RequestMapping("/getEventsByTitle")
    public List<Events> getEventsByTitle(@RequestParam("search") String title,int index){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",title);
        wrapper.orderByDesc("id");
        wrapper.last("limit "+(index*pageNum)+","+pageNum);
        return eventsService.list(wrapper);
    }

    /**
     * 根据查询获取总页数
     * @param title 查询标题
     * @return 页数
     */
    @RequestMapping("/getEventsByTitleSize")
    public int getEventsByTitleSize(@RequestParam("search")String title){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",title);
        wrapper.orderByDesc("id");
        int pageSize = eventsService.count(wrapper);
        if (pageSize<pageNum){
            return 1;
        }
        return (int)Math.ceil((double) pageSize/pageNum);
    }
}
